<?php

	class content {
		
		private $content = "";
		private $cfg;
		
		public function __construct(){
			
			if(file_exists("CodeCleanerConfig.php")){
				require_once("CodeCleanerConfig.php");
				$this -> cfg = $cfg;
				ob_start();
			}
			else {
				//exit;
			}
			
		}
		
		private function _doMagic(){
			
			/* 
			
				I teraz będzie wyglądać to tak, bo mi się już NIE CHCE
				1. Wyrzucacie ten koment
				2. Tworzycie tu nowe instancje swoich klas, or whateva
				3. W $content jest cały sajt
				4. Robicie co tam sobie chcecie
				5. A potem jest wywołanie print()
				6. Dziękujędobranocentersossie
			
			*/
			
			$this -> _print();
			
		}
		
		private function _print(){
			
			echo $this -> content;
			
		}
		
		public function __destruct(){
			
			$this -> content = ob_get_clean();
			
			$this -> _doMagic();
			
		}
		
	}

	class codeCleaner extends ccPaint {
		
		private $action = null;
		private $password = null;
		protected $cfg = null;
		
		public function __construct(){
			
			$this -> myConstruct();
			session_start();
			(isset($_GET["codecleaner"])) ? $this -> action = $_GET["codecleaner"] : $this -> action = "page";
			
			switch($this -> action){
				
				case "":
					$this -> _init();
					break;
				case "auth":
					$this -> _checkAuthorization();
					break;
				case "logout":
					$this -> _logout();
				case "page":
					break;
				case "lang":
					$this -> _setLang();
					break;
				case "createcfg":
					$this -> _configCreator();
					break;
				case "save":
					$this -> _save();
					break;
				case "else":
					$this -> costamsezrob();
					break;
				
			}
			
		}
		
		private function _init(){
			
			$this -> _includeConfig();
			$this -> _getLang();
			$this -> _authorize();
			
		}
		
		private function _setLang(){
			
			setcookie("cc_lang", $_GET["lang"], time() + 60 * 60 * 24 * 7);
			header("Location: ?codecleaner");
			
		}
		
		private function _getLang(){
			
			$this -> lang = (isset($_COOKIE["cc_lang"]) ? $_COOKIE["cc_lang"] : "pl");
			
		}
		
		private function _includeConfig(){
			
			if(file_exists("CodeCleanerConfig.php")){
				require_once("CodeCleanerConfig.php");
				$this -> cfg = $cfg;
			}
			else {
				header("Location: ?codecleaner=createcfg");
			}
			
		}
		
		private function _checkAuthorization(){
			
			$this -> _includeConfig();
			$this -> password = $_POST["password"];
			if($this -> password == $this -> cfg["password"]){
				$_SESSION["cc_auth"] = $this -> cfg["password"];
				header("Location: ?codecleaner");
			}
			else {
				$this -> _invalidPassword();
			}
			
		}
		
		private function _configCreator(){
			
			echo "dupa";
			
		}
		
		private function _save(){
			
			$tpl = new ccTpl();
			$newCfg = $tpl -> bool($this -> configProto, $_POST);
			$file = fopen("CodeCleanerConfig.php", "w+");
			fwrite($file, $newCfg);
			header("Location: ?codecleaner");
			
		}
		
		private function _invalidPassword(){
			
			echo "buuu!";
			
		}
		
		private function _authorize(){
			
			if(isset($_SESSION["cc_auth"]) && $_SESSION["cc_auth"] == $this -> cfg["password"]){
				$this -> authorized = true;
			}
			else {
				$this -> authorized = false;
			}
			
		}
		
		private function _logout(){
			
			session_destroy();
			header("Location: ?codecleaner");
			
		}
		
	}

	class ccPaint {
				
		protected $authorized = false;
		protected $lang = "pl";
				
		public $loginPage = '<!DOCTYPE html> 

		<html>
		<head>        
			<meta http-equiv="content-type" content="text/html; charset=UTF-8" />  
			<link rel="shortcut icon" href="" type="image/x-icon" />
			<title>ENTERSO CodeCleaner - Logowanie</title> 
		</head>

		<body>

		<div id="login">
			<div class="login_box">
				<form method="post" action="?codecleaner=auth">
					<fieldset>
						<legend>{lang:login_form}</legend>
						<label for="login_input">{lang:password}</label>
						<input id="login_input" class="" type="password" name="password" value="" />
						<input class="login_submit" type="submit" value="{lang:login}" />
					</fieldset>
				</form>
				<ul class="lang_select">
					<li>
						<a href="?codecleaner=lang&amp;lang=pl">PL</a>
					</li>
					<li>
						<a href="?codecleaner=lang&amp;lang=en">EN</a>
					</li>
					<li>
						<a href="?codecleaner=lang&amp;lang=de">DE</a>
					</li>
				</ul>
			</div>
		</div>

		</body>
		<style type="text/css">

			body {
				font-family: "lucida grande", tahoma, verdana, arial, sans-serif;
				font-size: 0.8em;
			}

			#login {
				left: 50%;
				margin-left: -359px;
				position: fixed;
				top: 50%;
				margin-top: -65px;
			}

			.login_box {
				width: 718px;
				height: 90px;
				margin: 0 auto;
				border: 0;
				padding: 20px;
			}

			legend {
				border: 1px solid #D7DCE9;
				padding: 2px 15px;
				text-align: left;
			}

			fieldset {
				border: 1px solid #D7DCE9;
				margin: 0;
				padding: 20px;
				text-align: center;
			}

			.login_submit {
				border: 1px solid #D7DCE9;
				background-color: white;
				padding: 1px 10px 1px 10px;
				height: 20px;
			}

			#login_input {
				border: 1px solid #D7DCE9;
				height: 20px;
				padding: 0;
			}

			.lang_select {
				list-style: none;
				margin: 0px 0px 0px 0px;
				padding: 0px 0px 0px 0px;
				height: 21px;
			}

			.lang_select li {
				display: inline-block;
				border-left: 1px solid #D7DCE9;
				border-right: 1px solid #D7DCE9;
				border-bottom: 1px solid #D7DCE9;
				padding: 0 0;
				margin: 0 0 0 0;
				height: 20px;
				line-height: 20px;
				text-align: center;
			}

			.lang_select a {
				display: block;
				width: 27px;
				text-decoration: none;
				color: #000;
			}

			.lang_select a:hover {
				background-color: #f2f2f2;
			}

		</style>
		</html>';
		
		public $backendPage = '<!DOCTYPE html> 

		<html>
		<head>        
			<meta http-equiv="content-type" content="text/html; charset=UTF-8" />  
			<link rel="shortcut icon" href="" type="image/x-icon" />
			<title>ENTERSO CodeCleaner - Panel administracyjny</title> 
		</head>

		<body>

		<div id="wrapper">
			<ul class="cc_tabs_menu">
				<li class="active menu_elem">{lang:tab_codecleaner}</li>
				<li class="menu_elem">{lang:tab_css}</li>
				<li class="menu_elem">{lang:tab_javascript}</li>
				<li class="menu_elem">{lang:tab_headers}</li>
				<li class="menu_elem">{lang:tab_seo}</li>
				<li class="menu_elem">{lang:tab_help}</li>
				<a href="?codecleaner=logout" class="menu_elem">{lang:logout}</a>
			</ul>

			<form method="post" action="?codecleaner=save">
				<fieldset class="fieldset_box">
					<legend class="hide">CodeCleaner</legend>
					<div class="fields">
						<label for="active">{lang:cc_active}</label>
						<input type="checkbox" value="1" name="cc_active" id="active" class="checkbox" {checkbox:cc_active} />     
					</div>
					<div class="fields">
						<label for="password">{lang:cc_change_password}</label>
						<input type="password" value="" name="password" id="password" class="input" />   
					</div>
				</fieldset>
				<fieldset class="fieldset_box hide">
					<legend class="hide">CSS</legend>
					<div class="fields">
						<label for="gzip_css">{lang:css_convert_colors_to_hex}</label>
						<input type="checkbox" value="1" name="css_convert_colors_to_hex" class="checkbox" {checkbox:css_convert_colors_to_hex} />     
					</div>
					<div class="fields">
						<label for="gzip_css">{lang:css_shorten_hex_codes}</label>
						<input type="checkbox" value="1" name="css_shorten_hex_codes" class="checkbox" {checkbox:css_shorten_hex_codes} />     
					</div>
					<div class="fields">
						<label for="gzip_css">{lang:css_convert_hex_to_colors}</label>
						<input type="checkbox" value="1" name="css_convert_hex_to_colors" class="checkbox" {checkbox:css_convert_hex_to_colors} />     
					</div>
					<div class="fields">
						<label for="gzip_css">{lang:css_convert_rgb_to_hex}</label>
						<input type="checkbox" value="1" name="css_convert_rgb_to_hex" class="checkbox" {checkbox:css_convert_rgb_to_hex} />     
					</div>
					<div class="fields">
						<label for="gzip_css">{lang:css_remove_empty_attrs}</label>
						<input type="checkbox" value="1" name="css_remove_empty_attrs" class="checkbox" {checkbox:css_remove_empty_attrs} />     
					</div>
					<div class="fields">
						<label for="gzip_css">{lang:css_merge_identical_rules}</label>
						<input type="checkbox" value="1" name="css_merge_identical_rules" class="checkbox" {checkbox:css_merge_identical_rules} />     
					</div>
					<div class="fields">
						<label for="gzip_css">{lang:css_merge_identical_attrs}</label>
						<input type="checkbox" value="1" name="css_merge_identical_attrs" class="checkbox" {checkbox:css_merge_identical_attrs} />     
					</div>
					<div class="fields">
						<label for="gzip_css">{lang:css_merge_props}</label>
						<input type="checkbox" value="1" name="css_merge_props" class="checkbox" {checkbox:css_merge_props} />     
					</div>
					<div class="fields">
						<label for="gzip_css">{lang:css_remove_overwritten_props}</label>
						<input type="checkbox" value="1" name="css_remove_overwritten_props" class="checkbox" {checkbox:css_remove_overwritten_props} />     
					</div>
					<div class="fields">
						<label for="gzip_css">{lang:css_shorten_margin_padding_props}</label>
						<input type="checkbox" value="1" name="css_shorten_margin_padding_props" class="checkbox" {checkbox:css_shorten_margin_padding_props} />     
					</div>
					<div class="fields">
						<label for="gzip_css">{lang:css_change_txts_to_numbers}</label>
						<input type="checkbox" value="1" name="css_change_txts_to_numbers" class="checkbox" {checkbox:css_change_txts_to_numbers} />     
					</div>
					<div class="fields">
						<label for="gzip_css">{lang:css_show_stats}</label>
						<input type="checkbox" value="1" name="css_show_stats" class="checkbox" {checkbox:css_show_stats} />     
					</div>
					<div class="fields">
						<label for="gzip_css">{lang:css_merge_css_files}</label>
						<input type="checkbox" value="1" name="css_merge_css_files" class="checkbox" {checkbox:css_merge_css_files} />     
					</div>
				</fieldset>
				<fieldset class="fieldset_box hide">
					<legend class="hide">JavaScript</legend>
					<div class="fields">
						<label for="gzip_js">{lang:js_gzip_compression}</label>
						<input type="checkbox" value="1" name="js_gzip_compression" id="gzip_js" class="checkbox" {checkbox:js_gzip_compression} />     
					</div>
					<div class="fields">
						<label for="whitespace_js">{lang:js_remove_whitespaces}</label>
						<input type="checkbox" value="1" name="js_remove_whitespaces" id="js_remove_whitespaces" class="checkbox" {checkbox:js_remove_whitespaces} />   
					</div>
					<div class="fields">
						<label for="merge_js">{lang:js_merge_js_files}</label>
						<input type="checkbox" value="1" name="js_merge_js_files" id="merge_js" class="checkbox" {checkbox:js_merge_js_files} />   
					</div>
				</fieldset>
				<fieldset class="fieldset_box hide">
					<legend class="hide">Headers</legend>
					<div class="fields">
						<label for="">Header A</label>
						<input type="checkbox" value="1" name="" id="" class="checkbox" />     
					</div>
					<div class="fields">
						<label for="">Header B</label>
						<input type="checkbox" value="1" name="" id="" class="checkbox" />   
					</div>
					<div class="fields">
						<label for="">Header C</label>
						<input type="checkbox" value="1" name="" id="" class="checkbox" >   
					</div>
				</fieldset>
				<fieldset class="fieldset_box hide">
					<legend class="hide">SEO</legend>
					<div class="fields">
						<label for="add_alt_attr">{lang:seo_complete_alt}</label>
						<input type="checkbox" value="1" name="seo_complete_alt" id="add_alt_attr" class="checkbox" {checkbox:seo_complete_alt} />   
					</div>
					<div class="fields">
						<label for="add_title_attr">{lang:seo_complete_title}</label>
						<input type="checkbox" value="1" name="seo_complete_title" id="add_title_attr" class="checkbox" {checkbox:seo_complete_title} />
					</div>
				</fieldset>
				<fieldset class="fieldset_box hide">
					<legend class="hide">Pomoc</legend>
					<p>help tab...</p>
				</fieldset>
				<fieldset class="fieldset_box fieldset_save">
					<legend class="legend_box">{lang:cc_save}</legend>
					<div class="fields">
						<input type="hidden" name="save" value="1" />
						<input type="submit" name="submit" class="submit" value="{lang:cc_save}" />
					</div>
				</fieldset>
			</form>
		</div>

		</body>  
		<style type="text/css">

			body {
				font-family: "lucida grande", tahoma, verdana, arial, sans-serif;
				font-size: 0.8em;
			}

			#wrapper {
				width: 600px;
				margin: 0 auto;
			}

			.cc_tabs_menu {
				list-style: none;
				margin: 0px 0px 0px 0px;
				padding: 0px 0px 0px 0px;
				border-bottom: 1px solid #D7DCE9;
				height: 21px;
			}

			.cc_tabs_menu li, .cc_tabs_menu a {
				display: inline-block;
				border-top: 1px solid #D7DCE9;
				border-left: 1px solid #D7DCE9;
				border-right: 1px solid #D7DCE9;
				padding: 0 10px;
				cursor: pointer;
				cursor: pointer;
				height: 20px;
				line-height: 20px;
			}

			.cc_tabs_menu a, .cc_tabs_menu a:visited {
				color: #E5E5E5;
				text-decoration: none;
			}

			.cc_tabs_menu a:hover {
				color: #000;
			}

			.active {
				border-bottom: 1px solid #FFF;
			}

			.fieldset_box {
				border: 1px solid #D7DCE9;
				border-top: 0px;
				margin: 0;
			}

			.fieldset_save {
				margin-top: 15px;
				border: 1px solid #D7DCE9;
			}

			.legend_box {
				border: 1px solid #D7DCE9;
				padding: 2px 15px;
			}

			.fields {
				margin: 5px 0 2px 0;
			}       

			.fieldset legend {
				padding: 0 5px;
			} 

			.fields label {
				margin: 0 15px 0 25px; 
				width: 370px;
				float: left;
			}

			.checkbox {
				/*float: left;
				margin: 3px 15px 0 25px; */
			} 

			.submit {
				margin: 0 15px 0 25px;
			}

			.hide {
				display: none;
			}

		</style>
		<script type="text/javascript">

			var menu_tabs = getElementsByClassName("menu_elem", document.getElementById("wrapper"));
			var fieldset_tabs = getElementsByClassName("fieldset_box", document.getElementById("wrapper"));
			var active_menu = menu_tabs[0];
			var active_fieldset = fieldset_tabs[0];

			for(var i in menu_tabs){
				menu_tabs[i].addEventListener("click", activateMenu, false);
			}

			function activateMenu(){
				if(this == menu_tabs[menu_tabs.length - 1]){
					return false;
				}
				active_menu.className = "";
				active_menu = this;
				active_menu.className = "active";

				active_fieldset.style.display = "none";
				active_fieldset = fieldset_tabs[menu_tabs.indexOf(active_menu)];
				active_fieldset.style.display = "block";
			}

			function getElementsByClassName(classname, node){
			    if(!node){
					node = document.getElementsByTagName("body")[0];
				}
			    var a = [];
				var backb = "\\\b";
			    var re = new RegExp(backb + classname + backb);
			    var els = node.getElementsByTagName("*");
			    for(var i = 0, j = els.length; i < j; i++){
			        if(re.test(els[i].className)){
						a.push(els[i]);
					}
				}
			    return a;
			}

		</script>
		</html>';
		
		public $configProto = '<?php

			$cfg = array(
				"cc_active" => {bool:cc_active},
				"password" => "admin",
				"css_convert_colors_to_hex" => {bool:css_convert_colors_to_hex},
				"css_shorten_hex_codes" => {bool:css_shorten_hex_codes},
				"css_convert_hex_to_colors" => {bool:css_convert_hex_to_colors},
				"css_convert_rgb_to_hex" => {bool:css_convert_rgb_to_hex},
				"css_remove_empty_attrs" => {bool:css_remove_empty_attrs},
				"css_merge_identical_rules" => {bool:css_merge_identical_rules},
				"css_merge_identical_attrs" => {bool:css_merge_identical_attrs},
				"css_merge_props" => {bool:css_merge_props},
				"css_remove_overwritten_props" => {bool:css_remove_overwritten_props},
				"css_shorten_margin_padding_props" => {bool:css_shorten_margin_padding_props},
				"css_change_txts_to_numbers" => {bool:css_change_txts_to_numbers},
				"css_show_stats" => {bool:css_show_stats},
				"css_merge_css_files" => {bool:css_merge_css_files},
				"js_gzip_compression" => {bool:js_gzip_compression},
				"js_remove_whitespaces" => {bool:js_remove_whitespaces},
				"js_merge_js_files" => {bool:js_merge_js_files},
				"seo_complete_alt" => {bool:seo_complete_alt},
				"seo_complete_title" => {bool:seo_complete_title}
			);

		?>';
		
		public function myConstruct(){
			
			ob_start();
			
		}
		
		public function paint(){
			
			$tpl = new ccTpl();
			
			if($this -> authorized){
				$this -> backendPage = $tpl -> init($this -> backendPage, $this -> lang, "backend", $this -> cfg);
				echo $this -> backendPage;
			}
			else if(!$this -> authorized){
				$this -> loginPage = $tpl -> init($this -> loginPage, $this -> lang, "login", $this -> cfg);
				echo $this -> loginPage;
			}
			else {
				die("Paint error");
			}
			
		}
		
		public function __destruct(){
			
			ob_end_clean();
			$this -> paint();
			
		}
		
	}
	
	class ccTpl {
		
		private $lang = array(
			"pl" => array(
				"login" => array(
					"login_form" 	=> "Logowanie",
					"password" 		=> "Hasło",
					"login" 		=> "Zaloguj" 
				),
				"backend" => array(
					"tab_codecleaner"	 				=> "CodeCleaner",
					"tab_css" 							=> "CSS",
					"tab_javascript"					=> "Javacript",
					"tab_headers"						=> "Nagłówki",
					"tab_seo"							=> "SEO",
					"tab_help"							=> "Pomoc",
					"logout"							=> "Wyloguj",
					"cc_active"							=> "CodeCleaner aktywny",
					"cc_change_password"				=> "Zmień hasło",
					"css_convert_colors_to_hex"			=> "Konwertuj nazwy kolorów do HEX",
					"css_shorten_hex_codes"				=> "Skróć kody HEX",
					"css_convert_hex_to_colors"			=> "Zmień kody HEX na kolory",
					"css_convert_rgb_to_hex"			=> "Zmień kolory RGB na HEX",
					"css_remove_empty_attrs"			=> "Usuń puste atrybuty",
					"css_merge_identical_rules"			=> "Połącz identyczne reguły",
					"css_merge_identical_attrs"			=> "Połącz identyczne atrybuty",
					"css_merge_props"					=> "Połącz właściwości",
					"css_remove_overwritten_props"		=> "Usuń nadpisane właściwości",
					"css_shorten_margin_padding_props"	=> "Skróć właściwości margin i padding",
					"css_change_txts_to_numbers"		=> "Zmień wagi tekstowe na liczby",
					"css_show_stats"					=> "Pokaż statystyki",
					"css_merge_css_files"				=> "Połącz CSS w jeden plik",
					"js_gzip_compression"				=> "Kompresja GZIP",
					"js_remove_whitespaces"				=> "Usuń białe znaki",
					"js_merge_js_files"					=> "Połącz pliki",
					"headers_a"							=> "Header A",
					"headers_b"							=> "Header B",
					"headers_c"							=> "Header C",
					"seo_complete_alt"					=> "Uzupełnij ALT",
					"seo_complete_title"				=> "uzupełniaj TITLE",
					"cc_save"							=> "Zapisz"
				)
			),
			"en" => array(
				"login" => array(
					"login_form" 	=> "Please login",
					"password" 		=> "Password",
					"login" 		=> "Login" 
				)	,
				"backend" => array(
					"tab_codecleaner" 	=> "CodeCleaner",
					"tab_css" 			=> "CSS",
					"tab_javascript"	=> "Javacript",
					"tab_headers"		=> "Headers",
					"tab_seo"			=> "SEO",
					"tab_help"			=> "Help",
					"logout"			=> "Logout"
				)
			)
		);
		
		public function init($content, $lang, $page, $cfg = null){
			
			foreach($this -> lang[$lang][$page] as $key => $val){
				$content = $this -> _translate($content, $key, $val);
			}
			
			$content = $this -> _checkbox($content, $cfg);
			
			return $content;
			
		}
		
		private function _translate($content, $key, $val){
			
			$pattern = '/{lang:' . $key  . '}/';
			$replacement = $val;
			
			return preg_replace($pattern, $replacement, $content);
			
		}
		
		public function bool($content, $arr){
			
			foreach($arr as $key => $val){
				$pattern = '/{bool:' . $key . '}/';
				$content = preg_replace($pattern, "1", $content);
			}
			
			$pattern = "/\{bool:.+\}/";
			$content = preg_replace($pattern, "0", $content);
			
			return $content;
			
		}
		
		private function _checkbox($content, $cfg){
			
			foreach($cfg as $key => $val){
				if($val == 1){
					$pattern = '/{checkbox:' . $key . '}/';
					$content = preg_replace($pattern, "checked=\"checked\"", $content);
				}
				else {
					$pattern = "/\{checked:.+\}/";
					$content = preg_replace($pattern, "", $content);
				}
			}
			
			return $content;
			
		}
		
	}
	
	if(!isset($_GET["codecleaner"])){
		$cc = new content();
	}
	else {
		$cc = new codeCleaner();
	}

?>